#pragma once

#include "AsyncLibDef.h"

namespace AsyncLib
{

class ASYNCLIB_API CAssocStateBase
{
public:
	CAssocStateBase();
	~CAssocStateBase();

	CAssocStateBase(const CAssocStateBase&) = delete;
	CAssocStateBase& operator=(const CAssocStateBase&) = delete;

	enum Status { Pending, Completed, Canceled };

	Status SetCallback(std::function<void(bool)>&& fnCallback);
	bool IsCompleted() const;
	void Forget();
	bool Wait();
	void Complete();
	void CaptureThread();
	void SetException(const std::exception_ptr& exception);
	bool MoveException(CAssocStateBase& stateFrom);

protected:
	void RethrowException();

	mutable std::mutex m_mutex;
	Status m_status;

private:
	ATID m_atid;
	std::function<void(bool)> m_fnCallback;
	std::exception_ptr m_exception;
	bool m_bExIgnored;
	bool m_bExHandled;
};

} // namespace AsyncLib
